package com.wiegand.content.contents {
	import com.caurina.transitions.Tweener;
	import com.wiegand.Configuration;
	import com.wiegand.content.AbstractContent;
	import com.wiegand.content.contents.projectcontent.ProjectDetails;
	import com.wiegand.content.contents.projectcontent.ProjectItem;
	import com.wiegand.content.contents.projectcontent.ProjectScroller;
	import com.wiegand.event.ClickEvent;
	import com.wiegand.gui.text.CssTextSprite;

	import flash.display.Sprite;

	/**
	 * @author gandy
	 */
	 
	public class ProjectContent extends AbstractContent {
		
		private var _proScroller : ProjectScroller;
		private var _proDetails : ProjectDetails;

		public function ProjectContent(xml:XMLList) {
			super(xml);
		}
		
		
		override public function init() : void {
			_container = new Sprite();
			_container.alpha = 0;
			addChild(_container);
			 
			var t : Sprite = CssTextSprite.create( String(_xml.text), "logo"  );
			t.x = 200;
			t.y = 50;
			_container.addChild(t);
			
			_proScroller = new ProjectScroller(_xml.projects, int(Math.round(Configuration.getInstance().stage.stageHeight*0.75)), int(_xml.projects.@width) );
			addChild(_proScroller);
			_proScroller.x = (Configuration.getInstance().stage.stageWidth/2)-(_proScroller.contentWidth/2);
		
			_proDetails = new ProjectDetails();
			addChild(_proDetails);
		}

		override public function open(delay : Number = 0) : void {
			Tweener.removeTweens(_container);
			Tweener.addTween(_container,{alpha:1,delay:delay,time:1,transition:"linear",onComplete:callOpen});
			_proScroller.open(delay+1);
			_proScroller.addEventListener(ProjectItem.EVENT_ACTION, projectClick);
		}
		
		private function projectClick(e:ClickEvent) : void {
			trace(e.value);
			trace( _xml.projects.project.(@id == e.value) );
			
			_proScroller.close(0);
			_proScroller.removeEventListener(ProjectItem.EVENT_ACTION, projectClick);
			
			_proDetails.updateData( _xml.projects.project.(@id == e.value) );
			_proDetails.open(1);
		}

		override public function close(delay : Number = 0) : void {
			
			_proDetails.close(delay);
			
			_proScroller.close(delay);
			_proScroller.removeEventListener(ProjectItem.EVENT_ACTION, projectClick);
			
			Tweener.removeTweens(_container);
			Tweener.addTween(_container,{alpha:0,delay:delay+2,time:1,transition:"linear",onComplete:callClosed});
		}
		
		override public function resize() : void {
			_proScroller.x = (Configuration.getInstance().stage.stageWidth/2)-(_proScroller.contentWidth/2);
			var h : int = int(Math.round(Configuration.getInstance().stage.stageHeight*0.75));
			_proScroller.resize(h);
		}
		
		override public function destroy() : void {
			_proScroller.destroy();
			_proDetails.destroy();
			Tweener.removeTweens(_container);
			removeChild(_container);
		}
		
	}
}
